package com.lanou.projecta.news.science;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.StackView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.projecta.R;
import com.lanou.projecta.WebViewActivity;
import com.lanou.projecta.news.science.ScienceAdapter;
import com.lanou.projecta.news.science.ScienceBase;

import java.util.List;

/**
 * Created by dllo on 16/1/13.
 */
public class NewsScience extends Fragment{
    private ScienceBase scienceBases;
    private List<ScienceBase.T1348649580692Entity> base;
    private ScienceAdapter adapter;
    private ListView listView;
    private RequestQueue queue;
    private String url = "http://c.3g.163.com/nc/article/headline/T1348649580692/0-20.html";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news_science,null);
        listView = (ListView) view.findViewById(R.id.science_listView);
        queue = Volley.newRequestQueue(getActivity());

        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson =  new Gson();
                scienceBases = gson.fromJson(response,ScienceBase.class);
                base = scienceBases.getT1348649580692();
                base.remove(0);
                adapter = new ScienceAdapter(getActivity(),base);
                listView.setAdapter(adapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        String url = base.get(i).getUrl_3w();
                        Intent intent = new Intent(getActivity(), WebViewActivity.class);
                        intent.putExtra("webView", url);
                        startActivity(intent);
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
       queue.add(request);
        return view;
    }
}
